package com.cloud.code.牛客网;

import java.util.*;

public class Test45 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String[] s = scanner.nextLine().split(" ");
            int zuoYe = Integer.parseInt(s[0]);
            int n = Integer.parseInt(s[1]);
            int[] nums = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = scanner.nextInt();
            }

            List<Integer> list = new ArrayList<>();
            Arrays.sort(nums);
            list.add(nums[0]);
            list.add(nums[1]);
            list.add(nums[2]);
            for (int i = 3; i < nums.length; i++) {
                Collections.sort(list);
                int i1 = list.get(0) + nums[i];
                list.remove(0);
                list.add(i1);
            }

            System.out.println(list);


        }
    }
}
